package com.cxtj;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;



@Controller
@RequestMapping("/CxtjController")
public class CxtjController {
	
	@Resource(name = "CxtjDAO")	
	private CxtjDAO cxtjDAO;
	
	@Resource(name = "UserDAO")	
	private UserDAO userDAO;	
	
	@ResponseBody
	@RequestMapping(value = "/cx", produces = "application/json;charset=UTF-8")
	public HashMap<String,Object> cxData(HttpServletRequest request,String page,String keyWord,String userName,String mintime,String maxtime)  throws UnsupportedEncodingException{
		
		HashMap<String,Object> returnMap = cxtjDAO.selectPage(page, keyWord,userName,mintime,maxtime);		
		return returnMap;		
	}
	
	@ResponseBody
	@RequestMapping(value = "/getPie", produces = "application/json;charset=UTF-8")
	public Map<String, Object> getPie(HttpServletRequest request,String page,String keyWord,String userName,String mintime,String maxtime)  throws UnsupportedEncodingException{
		Map<String, Object> returnMap = new HashMap<String, Object>();
		List<Map<String, Object>> datas = cxtjDAO.getPieData();		
		List<String> items = new ArrayList<String>();
		for(Map<String, Object> map:datas){
			items.add(map.get("name").toString());
		}
	//	System.out.println(items.toString());
		returnMap.put("datas", datas);
		returnMap.put("items", items);
		return returnMap;		
	}
	@ResponseBody
	@RequestMapping(value = "/getBar", produces = "application/json;charset=UTF-8")
	public Map<String, Object> getBar(HttpServletRequest request,String page,String keyWord,String userName,String mintime,String maxtime)  throws UnsupportedEncodingException{
		Map<String, Object> returnMap = new HashMap<String, Object>();
		List<Map<String, Object>> datas = cxtjDAO.getBarData();		
		List<String> items = new ArrayList<String>();
		List<Map<String, Object>> series = new ArrayList<Map<String, Object>>();
		for(int i=0;i<datas.size();i++){  
			Map<String, Object> map=datas.get(i);		
			items.add(map.get("sqluser").toString());
			Map<String, Object> srsMap = new HashMap<String, Object>();
			srsMap.put("name", map.get("sqluser").toString());
			srsMap.put("type", "bar");			
			List<Integer> numList = new ArrayList<Integer>();
			for(String key:map.keySet()){
				if(key == "sqluser" || "sqluser".equals(key));
				else {
					numList.add(Integer.parseInt(map.get(key).toString()));
				}
			}	
			srsMap.put("data", numList);
			series.add(srsMap);
		}
	//	System.out.println(items.toString());			
		Set<String> tmp = datas.get(0).keySet();		
		tmp.remove("sqluser");
		List<String> titles	= new ArrayList<String>();	
		for(String title:tmp){
			String t= "20"+title.substring(0,2)+"-"+title.substring(2,4)+"-"+title.substring(4,6);
			titles.add(t);
		}				
		returnMap.put("titles", titles);	
		returnMap.put("items", items);	
		returnMap.put("series", series);	
		return returnMap;		
	}
	
	@ResponseBody
	@RequestMapping(value = "/getPieTb", produces = "application/json;charset=UTF-8")
	public Map<String, Object> getPieTb(HttpServletRequest request,String page,String keyWord,String userName,String mintime,String maxtime)  throws UnsupportedEncodingException{
		Map<String, Object> returnMap = new HashMap<String, Object>();
		List<Map<String, Object>> datas = cxtjDAO.getPieDataTb();		
		List<String> items = new ArrayList<String>();
		for(Map<String, Object> map:datas){
			items.add(map.get("name").toString());
		}
	//	System.out.println(items.toString());
		returnMap.put("datas", datas);
		returnMap.put("items", items);
		return returnMap;		
	}
	@ResponseBody
	@RequestMapping(value = "/getBarTb", produces = "application/json;charset=UTF-8")
	public Map<String, Object> getBarTb(HttpServletRequest request,String page,String keyWord,String userName,String mintime,String maxtime)  throws UnsupportedEncodingException{
		Map<String, Object> returnMap = new HashMap<String, Object>();
		List<Map<String, Object>> datas = cxtjDAO.getBarDataTb();		
		List<String> items = new ArrayList<String>();
		List<Map<String, Object>> series = new ArrayList<Map<String, Object>>();
		for(int i=0;i<datas.size();i++){  
			Map<String, Object> map=datas.get(i);		
			items.add(map.get("OBJ_NAME").toString());
			Map<String, Object> srsMap = new HashMap<String, Object>();
			srsMap.put("name", map.get("OBJ_NAME").toString());
			srsMap.put("type", "bar");			
			List<Integer> numList = new ArrayList<Integer>();
			for(String key:map.keySet()){
				if(key == "OBJ_NAME" || "OBJ_NAME".equals(key));
				else {
					numList.add(Integer.parseInt(map.get(key).toString()));
				}
			}	
			srsMap.put("data", numList);
			series.add(srsMap);
		}
	//	System.out.println(items.toString());			
		Set<String> tmp = datas.get(0).keySet();		
		tmp.remove("OBJ_NAME");
		List<String> titles	= new ArrayList<String>();	
		for(String title:tmp){
			String t= "20"+title.substring(0,2)+"-"+title.substring(2,4)+"-"+title.substring(4,6);
			titles.add(t);
		}				
		returnMap.put("titles", titles);	
		returnMap.put("items", items);	
		returnMap.put("series", series);	
		return returnMap;		
	}
	
	@ResponseBody
	@RequestMapping(value = "/getETL", produces = "application/json;charset=UTF-8")
	public Map<String, Object> getETL(HttpServletRequest request,String page){
		HashMap<String,Object> returnMap = userDAO.selectEtlPage(page);		
		return returnMap;				
	}
	
	@ResponseBody
	@RequestMapping(value = "/delETL", produces = "application/json;charset=UTF-8")
	public boolean delETL(HttpServletRequest request,String id){
//		System.out.println(id);
		return userDAO.deleteETL(id);				
	}
	
	@ResponseBody
	@RequestMapping(value = "/addETL", produces = "application/json;charset=UTF-8")
	public HashMap<String,Object> addETL(HttpServletRequest request,String tablename,String cnname,String username){
		return userDAO.insertETL(tablename,cnname,username);		 			
	//	return null;
	}
}
